package main.graph;

import java.util.ArrayList;
import java.util.HashMap;

import y.base.Edge;
import y.base.EdgeCursor;
import y.base.Node;
import y.base.NodeCursor;

import main.model.OntoNodePair;
import main.model.Tree2OntoMapping;

/**
 * 
 * represents a form tree with a mapping to an ontology
 * 
 * @author YuanAn
 * 
 */
public class FormTree extends ValuedGraph {
	private Tree2OntoMapping mapping;

	private HashMap<Edge, ArrayList<String>> edgeToTwoEndLabels; // holds the
																	// two ends
																	// of an
																	// edge

	public FormTree() {
		super();
		mapping = new Tree2OntoMapping(this);

		edgeToTwoEndLabels = new HashMap<Edge, ArrayList<String>>();
	}

	// make a copy
	public FormTree duplicateFormTree() {

		FormTree ans = new FormTree();

		// from original nodes to new nodes
		HashMap<Node, Node> origToNew = new HashMap<Node, Node>();

		for (NodeCursor nc = nodes(); nc.ok(); nc.next()) {
			Node origNode = nc.node();
			Node newNode = ans.createNode();
			origToNew.put(origNode, newNode);
			ans.putNodeValue(newNode, getNodeValue(origNode));
		}

		// from original edges to new edges
		HashMap<Edge, Edge> origEdgeToNewEdge = new HashMap<Edge, Edge>();

		for (EdgeCursor ec = edges(); ec.ok(); ec.next()) {
			Edge origEdge = ec.edge();
			Node origSource = origEdge.source();
			Node origTarget = origEdge.target();
			Node source = origToNew.get(origSource);
			Node target = origToNew.get(origTarget);
			Edge edge = ans.createEdge(source, target);
			ans.putEdgeValue(edge, getEdgeValue(origEdge));

			origEdgeToNewEdge.put(origEdge, edge);
		}
		
		return ans;
	}

	// getter and setter
	public Tree2OntoMapping getMapping() {
		return mapping;
	}

	public void setMapping(Tree2OntoMapping mapping) {
		this.mapping = mapping;
	}

	public void putEdgeToTwoNodeLabels(Edge e, ArrayList<String> endLabels) {
		edgeToTwoEndLabels.put(e, endLabels);
	}

	public ArrayList<String> getTwoEndLabels(Edge e) {
		return edgeToTwoEndLabels.get(e);
	}
}
